// Ejerforhold.cpp : Implementation of CEjerforhold
#include "stdafx.h"
#include "EBASdll.h"
#include "Ejerforhold.h"
#include "Database.h"
#include "AdresseImpl.h"

/////////////////////////////////////////////////////////////////////////////
// CEjerforhold

STDMETHODIMP CEjerforhold::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IEjerforhold
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CEjerforhold::get_Leder(BSTR *pVal)
{
	*pVal = m_leder.copy();

	return S_OK;
}

STDMETHODIMP CEjerforhold::put_Leder(BSTR newVal)
{
	m_leder = newVal;

	return S_OK;
}

STDMETHODIMP CEjerforhold::get_GUID(BSTR *pVal)
{
	*pVal = m_guid.copy();

	return S_OK;	
}

STDMETHODIMP CEjerforhold::SetInfo()
{
	_CommandPtr cmd;
	_RecordsetPtr recordset;

	const char * sql = "EBAS_SP_GEM_EJERFORHOLD";

	try {
		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandType = adCmdStoredProc;
		cmd->CommandText = sql;
		
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid));
		cmd->Parameters->Append( cmd->CreateParameter("guid", adVarChar, adParamInput, 255, m_guid));
		cmd->Parameters->Append( cmd->CreateParameter("leder", adVarChar, adParamInput, 255, m_leder));
		
		recordset = cmd->Execute(NULL, NULL, adCmdStoredProc);
		if (recordset->ADO_EOF)
			return E_ACCESSDENIED;

		m_guid = recordset->Fields->GetItem("GUID")->Value;
		m_leder = recordset->Fields->GetItem("LEDER")->Value;

		recordset->Close();

		if (m_pAdresse) {
			BSTR temp;
			CAdresseImpl * addr = new CAdresseImpl;
			if (NULL == addr)
				return E_OUTOFMEMORY;

			m_pAdresse->get_AdresseType(&temp);
			addr->put_AdresseType(temp);
			m_pAdresse->get_By(&temp);
			addr->put_By(temp);
			m_pAdresse->get_Email(&temp);
			addr->put_Email(temp);
			m_pAdresse->get_Gade(&temp);
			addr->put_Gade(temp);
			m_pAdresse->get_GUID(&temp);
			addr->put_GUID(temp);
			m_pAdresse->get_PostNummer(&temp);
			addr->put_PostNr(temp);
			m_pAdresse->get_Telefax(&temp);
			addr->put_Telefax(temp);
			m_pAdresse->get_Telefon(&temp);
			addr->put_Telefon(temp);
			
			addr->SetInfo(m_guid);
			
			if (addr)
				delete addr;
		}

	} catch (_com_error &e) {
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEjerforhold::get_Adresse(IAdresse **pVal)
{
	if (m_pAdresse) {
		*pVal = m_pAdresse;
		(*pVal)->AddRef();
	} else
		*pVal = NULL;

	return S_OK;
}

STDMETHODIMP CEjerforhold::put_Adresse(IAdresse *newVal)
{
	m_pAdresse = newVal;
	if (m_pAdresse)
		m_pAdresse->AddRef();

	return S_OK;
}

STDMETHODIMP CEjerforhold::Slet()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here

	return S_OK;
}
